<template>
  <div class="wrop">
    <div class="wrop">
      <a-table :columns="columns" :data-source="dataSource" bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="['name', 'age', 'address'].includes(column.dataIndex)">
            <div>
              <a-input
                v-if="editableData[record.key]"
                v-model:value="editableData[record.key][column.dataIndex]"
                style="margin: -5px 0"
              />
              <template v-else>{{ text }}</template>
            </div>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <div class="editable-row-operations">
              <span v-if="editableData[record.key]">
                <a-typography-link @click="save(record.key)">Save</a-typography-link>
                <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
                  <a>Cancel</a>
                </a-popconfirm>
              </span>
              <span v-else>
                <!-- <a class="mr-10" @click="edit(record.key)">Edit</a> -->
                <a @click="edit(record.name)">Edit</a>
                <a>Delete</a>
              </span>
            </div>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script setup>

import { cloneDeep } from 'lodash-es';
import {  reactive, ref } from 'vue';

const columns = [{
  title: 'name',
  dataIndex: 'name',
  width: '25%',
}, {
  title: 'age',
  dataIndex: 'age',
  width: '15%',
}, {
  title: 'address',
  dataIndex: 'address',
  width: '40%',
}, {
  title: 'operation',
  dataIndex: 'operation',
}];
const data = [];
for (let i = 0; i < 15; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

const dataSource = ref(data);
const editableData = reactive({});
// 编辑
const edit = name => {
  // editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
  editableData[name] = cloneDeep(dataSource.value.filter(item => name === item.name)[0]);
  console.log(name,'name');
};
// 保存
const save = key => {
  Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
  delete editableData[key];
};
// 取消
const cancel = key => {
  delete editableData[key];
};

</script>

<style lang="less" scoped>
.wrop {
  width: 100%;
}
</style>